#lang r5rs
(define (foldr op initial result)
  (if(null? result)
     initial
     (op (car result)
         (foldr op initial (cdr result)))))

(define (filter prediction l)
  (cond ((null? l) '())
        ((prediction (car l)) (cons (car l) (filter prediction (cdr l))))
        ((not(prediction (car l))) (filter prediction (cdr l)))))

(define (unique-pairs n)
  (define (enum-int-iter n x result)
    (if(> x n)
       result
       (enum-int n (+ 1 x) (cons x result))))
  (define (enum-int n) (enum-int-iter n 1 '()))
  (foldr append
         '()
         (map (lambda (x) (map (lambda (y) (list x y)) (enum-int (- x 1)))) (enum-int n))))
(define (prime-sum-pairs n)
  (define (sum-primes? x)
    (prime? (+ (car x) (cadr x)))) ;I'm sorry for my laziness
  (map (lambda (x) (list (car x) (cadr x) (+ (car x) (cadrx)))) (filter sum-primes? (unique-pairs n))))